package com.dunqi.gpm.shunqing.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.home.bean.YearPlan;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.NormalView;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Created by 空 on 2018/1/5 0005.
 */

public class YearAmountInfoFragment extends BaseFragment {


    @BindView(R.id.title_1_tv)
    TextView title1Tv;
    @BindView(R.id.report_cycle_view)
    NormalView reportCycleView;
    @BindView(R.id.plan_start_time_view)
    NormalView planStartTimeView;
    @BindView(R.id.plan_end_time_view)
    NormalView planEndTimeView;
    @BindView(R.id.build_kind_view)
    NormalView buildKindView;
    @BindView(R.id.last_year_completed_money_view)
    NormalView lastYearCompletedMoneyView;
    @BindView(R.id.whole_year_content_tv)
    NormalView wholeYearContentTv;
    @BindView(R.id.title_2_tv)
    TextView title2Tv;
    @BindView(R.id.total_money_view)
    NormalView totalMoneyView;
    @BindView(R.id.center_money_view)
    NormalView centerMoneyView;
    @BindView(R.id.province_money_view)
    NormalView provinceMoneyView;
    @BindView(R.id.city_money_view)
    NormalView cityMoneyView;
    @BindView(R.id.county_money_view)
    NormalView countyMoneyView;
    @BindView(R.id.bank_money_view)
    NormalView bankMoneyView;
    @BindView(R.id.get_money_view)
    NormalView getMoneyView;
    @BindView(R.id.private_money_view)
    NormalView privateMoneyView;
    @BindView(R.id.center_bank_money_view)
    NormalView centerBankMoneyView;
    @BindView(R.id.factory_bank_money_view)
    NormalView factoryBankMoneyView;
    @BindView(R.id.agricultural_bank_money_view)
    NormalView agriculturalBankMoneyView;
    @BindView(R.id.ccb_money_view)
    NormalView ccbMoneyView;
    @BindView(R.id.financing_view)
    NormalView financingView;
    @BindView(R.id.other_money_view)
    NormalView otherMoneyView;
    @BindView(R.id.determined_money_view)
    NormalView determinedMoneyView;
    @BindView(R.id.des_view)
    NormalView desView;
    @BindView(R.id.scrollView)
    ScrollView scrollView;

    private long projectId;
    private int yearPlan;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        ApiFactory.getInstance()
                .queryProjectAnnualPlanInfo(projectId, yearPlan)
                .compose(RxSchedulers.<Data<YearPlan>>io_main())
                .subscribe(new ComObserver<YearPlan>() {
                    @Override
                    public void onSuccess(YearPlan yearPlan) {
                        if (yearPlan == null) {
                            return;
                        }

                        refreshUi(yearPlan);

                    }

                    @Override
                    public void onFinal() {

                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                });
    }

    private void refreshUi(YearPlan yearPlan) {
        title1Tv.setText(yearPlan.getPlanYear() + "年年度计划");
        title2Tv.setText(yearPlan.getPlanYear() + "年年度投资计划(万元)");

        String reportFrequency = yearPlan.getReportFrequency();
        String text;
        if ("report_per_month".equals(reportFrequency)) {
            text = "按月汇报\n每月21日自动生成待汇报，汇报时限是7天";
        } else if ("report_per_10days".equals(reportFrequency)) {
            text = "按旬汇报\n每月9、19、29日自动生成待汇报，汇报时限是1天";
        } else {
            text = "无需汇报";
        }
        reportCycleView.setText(text);

        planStartTimeView.setText(yearPlan.getPlanStartTime());
        planEndTimeView.setText(yearPlan.getPlanCompleteTime());
        buildKindView.setText(yearPlan.getConstructionTypeText());
        lastYearCompletedMoneyView.setText(String.valueOf(yearPlan.getCompletedFundLastYear()));
        wholeYearContentTv.setText(yearPlan.getConstructionDescWholeYear());

        totalMoneyView.setText(String.valueOf(yearPlan.getTotalInvestmentAmount()));
        centerMoneyView.setText(String.valueOf(yearPlan.getNationalFund()));
        privateMoneyView.setText(String.valueOf(yearPlan.getProvinceFund()));
        cityMoneyView.setText(String.valueOf(yearPlan.getMunicipalFund()));
        countyMoneyView.setText(String.valueOf(yearPlan.getConuntyFund()));
        bankMoneyView.setText(String.valueOf(yearPlan.getBankLoadFund()));
        getMoneyView.setText(String.valueOf(yearPlan.getAttractInvestmentFund()));
        privateMoneyView.setText(String.valueOf(yearPlan.getSelfFund()));
        financingView.setText(String.valueOf(yearPlan.getRaisingFund()));
        otherMoneyView.setText(String.valueOf(yearPlan.getOtherFund()));
        determinedMoneyView.setText(String.valueOf(yearPlan.getUndeterminedFund()));
        desView.setText(String.valueOf(yearPlan.getFundSourceDesc()));

        centerBankMoneyView.setText(String.valueOf(yearPlan.getBocLoadFund()));
        factoryBankMoneyView.setText(String.valueOf(yearPlan.getIcbcLoadFund()));
        agriculturalBankMoneyView.setText(String.valueOf(yearPlan.getAbcLoadFund()));
        ccbMoneyView.setText(String.valueOf(yearPlan.getCcbLoadFund()));
    }

    @Override
    protected void initUiAndListener() {

    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();

        projectId = arguments.getLong(Constance.PROJECT_ID);
        yearPlan = arguments.getInt(Constance.YEAR);
    }


    public static YearAmountInfoFragment newInstance(int yearPlan, long projectId) {
        YearAmountInfoFragment fragment = new YearAmountInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECT_ID, projectId);
        bundle.putInt(Constance.YEAR, yearPlan);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_year_amount_info;
    }

}
